<?php

namespace Sharks\Core\Make;

use Sharks\Core\AppContext;
use Sharks\Core\Cfg\AppConfig;
use Sharks\Core\Persistence\DatabaseConnections;
use Sharks\Core\Persistence\Db;
use Sharks\Core\Support\Base\Argument;
use Sharks\Core\Support\Base\Arr;
use Sharks\Core\Support\Base\Precondition;

/**
 * 数据库初始化
 * 配置项为: databases
 * 示例
 * <pre>
 * 	databases:
 * 		***
 * </pre>
 *
 * 依赖：
 * 		AppConfigMaker
 */
class DatabaseMaker extends Maker {

	public function server(AppContext $app) {
		$databases = AppConfig::get('databases');

		Precondition::check(!empty($databases), '配置项中没有数据库的设置');
		foreach ($databases as $key => $value) {
			Db::set($value, $key);
		}
	}

	public function dev(AppContext $app) {
		$databases = AppConfig::get('databases');

		Precondition::check(!empty($databases), '配置项中没有数据库的设置');
		foreach ($databases as $key => $value) {
			Db::set($value, $key);
		}
	}

	/**
	 * 前置依赖
	 */
	public function makers() {
		return array(AppConfigMaker::class);
	}
}